<template>
  <Container>
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <export-btn @onExport="onExport" class="item" slot="extra"></export-btn>
      <TabPane label="门店月度积分返利" name="tab1">
        <homeTable
          :list="list"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
        />
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import IntegralMonthListData from './list/IntegralMonthListData'
import HeadFormItem from './list/HeadFormItem'
import homeTable from '@/components/homeTable'
import { clearEmpty, formatDate } from '@/util'
export default {
  name: 'report/integralmonth/list',
  components: {
    HeadFormItem,
    homeTable
  },
  data() {
    return {
      tab: "tab1",
      list: IntegralMonthListData
    };
  },
  mounted() {
    // this.list.currentPage = ~~this.$route.query.currentPage || 1;
    // this.list.pageSize = ~~this.$route.query.pageSize || this.list.pageSize;
    this.getData()
    this.$bus.$on('detailRow', ({row}) => {this.detailRow(row)})
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    onSearchBtnClick(form) {
      this.list.currentPage = 1
      this.list.form = form
      this.getData()
    },
    detailRow(row) {
      this.$router.push({
        name: 'report/integralmonth/detail',
        query: {
          id: row.id
        }
      })
    },
    getData() {
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam(),
        current: this.list.currentPage,
        size: this.list.pageSize
      }
      this.$ajaxGet("/assets/store_int_month/page", param).then(data => {
        this.list.data = [];
        this.list.totalItem = Number(data.total)
        this.list.data = (data.records || []).map(item => {
          return {
            ...item,
            address: (item.provinceName ? item.provinceName : '') + (item.cityName ? item.cityName : '') + (item.areaName ? item.areaName : '')
          }
        })
        this.list.status = "pass"
      }).catch(() => {
        this.list.status = "error"
      })
    },
    onPageChange(number) {
      this.list.currentPage = number
      this.getData();
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1
      this.list.pageSize = pageSize
      this.getData()
    },
    formatParam() {
      if (!this.list.form.time) {
        this.$Notice.warning({
          title: `请选择月份`
        })
        return
      }
      if (!this.list.form.status) {
        this.$Notice.warning({
          title: `请选择审核状态`
        })
        return
      }
      let month = formatDate(this.list.form.time).split('-')[1]
      let params = {
        year: formatDate(this.list.form.time).split('-')[0],
        month: month.charAt(0) == 0 ? month.charAt(1) : month,
        storeTypeId: this.list.form.storeTypeId ? this.list.form.storeTypeId : null,
        isTask: this.list.form.isTask ? eval(this.list.form.isTask) : null,
        provinceId: this.list.form.provinceId ? this.list.form.provinceId : null,
        cityId: this.list.form.cityId ? this.list.form.cityId : null,
        areaId: this.list.form.areaId ? this.list.form.areaId : null,
        distributorId: this.list.form.distributorId ? this.list.form.distributorId : null,
        status: this.list.form.status
      }
      return clearEmpty(params)
    },
    onExport() {
      if (this.list.data.length === 0) {
        this.$Notice.warning({
          title: `暂无数据，无法导出`
        })
        return
      }
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam()
      }
      this.$jsonPost('/assets/store_int_month/export', param).then(data => {
        this.list.status = 'pass'
        this.$Modal.success({
          title: '导出成功',
          content: '请到首页-导出列表处下载',
          onOk: () => {
            this.$router.push('/home/export/list')
          }
        })
      }).catch(err => {
        this.list.status = 'pass'
        this.$Notice.error({
          title: '导出失败'
        })
      })
    }
  }
}
</script>
